
const db = wx.cloud.database()
const lostArticle = db.collection("lostArticle")
const user = db.collection("user")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    time:'',
    site:'',
    phone:'',
    describe:'',
    userId:'',
    type:"1",
    fileList: [],
    authorization:false
  },
  // 上传
  uploader(){
    let date = new Date();
    let year = date.getFullYear(); //年
    let month = date.getMonth() + 1; //月
    let dates = date.getDate(); //日

    let that = this
    let name=this.data.name
    let time=this.data.time
    let site=this.data.site
    let phone=this.data.phone
    let describe=this.data.describe
    let img=this.data.fileList
    let userId=this.data.userId
    let sort=this.data.type
    let times = `${year}-${month}-${dates}`;
    let is=false
    let imgs=[]
    if(img.length==0){
      imgs=['cloud://ximu-2gn2eddo73a9534f.7869-ximu-2gn2eddo73a9534f-1314483057/1666685437673-22.webp']
    }else{
    img.forEach(item => {
     imgs.push(item.url) 
    });
    }
        lostArticle.add({
          data: {
            name,
            time,
            site,
            describe,
            phone,
            imgs,
            is,
            userId,
            sort,
            times,
          }
        }).then(res=>{
          // console.log(res);
          wx.showToast({ title: '发布成功', icon: 'none' })
          that.setData({
            name:'',
            time:'',
            site:'',
            phone:'',
            describe:'',
            fileList: []
          })
        })
    


  },
  // 排他
  onType(){
    if(this.data.type==1){
      this.setData({
         type:"2"
      })
    }else{
      this.setData({
        type:"1"
     })
    }
  },
  
  // 授权
shouquan(){
  wx.showModal({
    title:'需要获取你上传的图片(只读取上传的)',
    content: "用于挂载失物图片",
    confirmText:"上传",
    success:(res)=>{
      if(res.confirm){
        this.setData({
          authorization:true
        })
      }
    }
 })
},
  
// 上传图片
beforeRead(event) {
    const { file, callback } = event.detail;
     callback( file.type === 'image' );
},

afterRead( event) {
  const file = event.detail.file 
  
  //还没上传时将选择的图片的上传状态设置为加载    
  var that = this
  const fileList = that.data.fileList 
  fileList.push({})
  fileList[ fileList.length-1 ].status='uploading'
  that.setData({ fileList })

  this.uploadImg( file.url )
},

// 上传图片到云开发的存储中
uploadImg( fileURL) {
  var that = this
  //上传文件
  const filePath = fileURL // 小程序临时文件路径
  const cloudPath = `${Date.now()}-${Math.floor(Math.random(0, 1) * 1000)}` + filePath.match(/\.[^.]+?$/)[0]
  // 给文件名加上时间戳和一个随机数，时间戳是以毫秒计算，而随机数是以 1000 内的正整数，除非 1 秒钟（1 秒=1000 毫秒）上传几十万张照片，不然文件名是不会重复的。
  // 将图片上传至云存储空间
  wx.cloud.uploadFile({
    cloudPath, // 指定上传到的云路径
    filePath // 指定要上传的文件的小程序临时文件路径
  }).then(res => {
    // 上传一张图片就会打印上传成功之后的 res 对象，里面包含图片在云存储里的 fileID，注意它的文件名和文件后缀
    console.log("res.fileID", res.fileID)
    // 上传完成需要更新 fileList
    const fileList = that.data.fileList 
    fileList[ fileList.length-1 ].url = res.fileID
    fileList[ fileList.length-1 ].status='done' //将上传状态修改为已完成
    that.setData({ fileList })
    console.log("fileList", fileList)
    wx.showToast({ title: '上传成功', icon: 'none' })
  }).catch((e) => {
    wx.showToast({ title: '上传失败', icon: 'none' })
    const fileList = that.data.fileList 
    fileList[ fileList.length-1 ].status='failed' //将上传状态修改为已完成
    that.setData({ fileList })
    // console.log(e)
  });
},

// 点击预览的x号，将图片删除
deleteImg( event) {
  // event.detail.index: 删除图片的序号值
  const delIndex = event.detail.index
const fileList = this.data.fileList 

  // 云存储删除（真删除）
  var fileID = fileList[ delIndex].url
  this.deleteCloudImg( fileID)
  
  // 页面删除（假删除）
  // 添加或删除 array.splice(index,howmany,item1,.....,itemX)
  fileList.splice( delIndex, 1)
  this.setData({ fileList })
},

// 删除云存储的图片
deleteCloudImg( fileID) {
  wx.cloud.deleteFile({
    fileList: [ fileID],
  }).then(res => {
    // handle success
    // console.log(res.fileList)
  }).catch((e) => {
    wx.showToast({ title: '删除失败', icon: 'none' })
    // console.log(e)
  })
},



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
       // 获取用户id
       user.where({              
        _id: 'e175419b635102f60005aade1b2ff3da',		
     })
     .get().then(res=>{					
      let appId=res.data.AppSecret
       wx.login({
        success: (res) => {
          let code = res.code
          wx.request({
            url: `https://api.weixin.qq.com/sns/jscode2session?appid=wxddc3210e3922b6e6&secret=${appId}&js_code=${code}&grant_type=authorization_code`,
            success: (res) => {
              // console.log(res);
              let openid = res.data.openid
              that.setData({
                userId:openid
              })
            }
          })
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})